#include "dialog_register.h"
#include "ui_dialog_register.h"


#include<QDebug>
#include <QHttpPart>
#include <qjsondocument.h>
#include <QJsonObject>
#include <QByteArray>
#include <QNetworkReply>
#include <QString>
#include <QHttpMultiPart>
#include <QUrl>
#include <QNetworkRequest>
#include <QDialogButtonBox>


Dialog_Register::Dialog_Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_Register)
{
    ui->setupUi(this);

    m_manager = new QNetworkAccessManager;

}

Dialog_Register::~Dialog_Register()
{
    delete ui;
}

void Dialog_Register::postRegister(QString user, QString pwd)
{
    QHttpMultiPart *dataPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart namePart;
    namePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"username\""));
    namePart.setBody(user.toLocal8Bit());
    dataPart->append(namePart);
    QHttpPart pwdtPart;
    pwdtPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\""));
    pwdtPart.setBody(pwd.toLocal8Bit());
    dataPart->append(pwdtPart);
    QUrl url("http://127.0.0.1:5000/register");
    QNetworkRequest netReq;
    netReq.setUrl(url);
    connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    m_manager->post(netReq,dataPart);

}

void Dialog_Register::on_pb_register_clicked()
{
    QStringList info_list;
    info_list << ui->le_username->text().trimmed();
    info_list << ui->le_password->text().trimmed();
    info_list << ui->le_password_confirm->text().trimmed();
    info_list << ui->le_phone->text().trimmed();
    qDebug() << info_list;

    if(info_list.at(0).isEmpty() && info_list.at(1).isEmpty())
    {
        QMessageBox::warning(this, "提示", "用户名或密码不能为空");
        return;
    }

    if(info_list.at(2).isEmpty())
    {
        QMessageBox::warning(this, "提示", "请输入确认密码");
        return;
    }

    if(info_list.at(1) != info_list.at(2))
    {
        QMessageBox::warning(this, "错误", "密码不一致");
        return;
    }

    postRegister(info_list.at(0), info_list.at(1));

    m_info = info_list;

    this->accept();
}

void Dialog_Register::on_pb_back_clicked()
{

    this->close();
}

void Dialog_Register::finishedSlot(QNetworkReply *reply)
{


    if (reply->error() == QNetworkReply::NoError)
    {

        QByteArray bytes = reply->readAll();

        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(bytes, &json_error));
        if(json_error.error != QJsonParseError::NoError)
        {
            return ;
        }
        QJsonObject rootObj = jsonDoc.object();
        int codeStr = rootObj.value("code").toInt();

        m_info << QString::number(codeStr);
        m_info << rootObj.value("message").toString();

        if(m_info.size() <= 4)
        {
            m_info << QString::fromUtf8(bytes);
        }

        // 将用户信息发送给主窗体
        emit register_info(m_info.at(0),m_info.at(1),m_info.at(3));
        emit register_info_list(m_info);

        qDebug()<< "code:" << codeStr;
        if (codeStr == 200)
        {


            accept();
        }
        else
        {
            //m_info.clear();
        }


    }




    reply->deleteLater();

}
